# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "家庭人身意外伤害保险"
    # 生成数据
    def genProductInfo(self):
        plan = {
            "planA": {
                "days": ["6m", "12m"],
                "people_count": [str(i) for i in range(1, 7)],
                # 家庭人身意外伤害保险
                "accident_family": ["100000"],
                # 意外医疗保险
                "accident_medical": ["10000"]
            },
            "planB": {

                "days": ["6m", "12m"],
                "people_count": [str(i) for i in range(1, 7)],
                # 家庭人身意外伤害保险
                "accident_family": ["200000"],
                # 意外医疗保险
                "accident_medical": ["20000"]

            }
        }
        for k in plan:
            for v in product(plan[k]["days"], plan[k]["people_count"], plan[k]["accident_family"],
                             plan[k]["accident_medical"]):
                data = Helper.formatProductInfo(plan[k], v)
                yield data

    # 执行爬虫
    def requestJson(self):
        for v in self.genProductInfo():

            if v["accident_family"] == "100000":
                data = {
                    "insuranceid_24333_保险金额": "100000",
                    "insuranceid_24334_保险金额": "10000",
                    "suiteId": "5882",
                    "period": "365",
                    "amount": "",
                    "personCount": "5",
                    "backInsuranceCount": "",
                    "youngAccident": "fmPersonalAccident",
                }
                data["period"] = "180" if v["days"] == "6m" else "365"
                data["personCount"] = v["people_count"]
            else:
                data = {
                    "insuranceid_24351_保险金额": "200000",
                    "insuranceid_24352_保险金额": "20000",
                    "suiteId": "5883",
                    "period": "365",
                    "amount": "",
                    "personCount": "5",
                    "backInsuranceCount": "",
                    "youngAccident": "fmPersonalAccident",
                }
                data["period"] = "180" if v["days"] == "6m" else "365"
                data["personCount"] = v["people_count"]
            payload = urlencode(data).replace("+", "")

            r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
            if r.status_code == 200 and "totalPremiun" in r.text:
                prices = [float(price["totalPremiun"]) for price in r.json()["insuranceList"]]
                price = Helper.decimalParse(sum(prices))
                data = {
                    "name": self.name,
                    "company": "北部湾财产保险",
                    "price": price
                }
                data.update(v)
                Helper.mergeTable(self.name, self.mysql, data)

